package com.hilin.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

@AllArgsConstructor
@NoArgsConstructor
public class BaseEntity<T> {
    @ApiModelProperty("开始时间，查询时间段的时候用，判断的是create_time字段")
    @TableField(exist = false)
    @Getter
    private String searchStartTime;
    @ApiModelProperty("结束时间，查询时间段的时候用")
    @TableField(exist = false)
    @Getter
    private String searchEndTime;

    @Setter
    @Getter
    @ApiModelProperty("like查询条件")
    @TableField(exist = false)
    @JsonIgnore
    private T likeCondition;

    public void setSearchEndTime(String searchEndTime) {
        if (!ObjectUtils.isEmpty(searchEndTime) && searchEndTime.length() < 19) {
            searchEndTime = searchEndTime + " 23:59:59";
        }
        this.searchEndTime = searchEndTime;
    }

    public void setSearchStartTime(String searchStartTime) {
        if (!ObjectUtils.isEmpty(searchStartTime) && searchStartTime.length() < 19) {
            searchStartTime = searchStartTime + " 00:00:00";
        }
        this.searchStartTime = searchStartTime;
    }

    @JsonIgnore
    public QueryWrapper<T> getQueryWrapper() throws IllegalAccessException {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        Field[] fields = getClass().getDeclaredFields();
        if (!ObjectUtils.isEmpty(fields)) {
            for (Field field : fields) {
                if (!ObjectUtils.isEmpty(field)) {
                    String tableFieldName = field.getName();
                    TableField tableField = field.getAnnotation(TableField.class);

                    if (tableFieldName.equalsIgnoreCase("likeCondition") || tableFieldName.equalsIgnoreCase("searchStartTime") || tableFieldName.equalsIgnoreCase("searchEndTime")) {
                        continue;
                    }

                    if (!ObjectUtils.isEmpty(tableField) && !ObjectUtils.isEmpty(tableField.value())) {
                        tableFieldName = tableField.value();
                    }
                    int modifier = field.getModifiers();
                    if ((modifier | Modifier.STATIC) == modifier) {
                        continue;
                    }
                    field.setAccessible(true);
                    Object value = field.get(this);
                    if (!ObjectUtils.isEmpty(value) && !ObjectUtils.isEmpty(tableFieldName)) {
                        queryWrapper.eq(tableFieldName, value);
                    }
                }
            }
        }

        if (!ObjectUtils.isEmpty(likeCondition)) {
            fields = likeCondition.getClass().getDeclaredFields();
            if (!ObjectUtils.isEmpty(fields)) {
                for (Field field : fields) {
                    if (!ObjectUtils.isEmpty(field)) {
                        String tableFieldName = field.getName();
                        TableField tableField = field.getAnnotation(TableField.class);

                        if (tableFieldName.equalsIgnoreCase("likeCondition") || tableFieldName.equalsIgnoreCase("searchStartTime") || tableFieldName.equalsIgnoreCase("searchEndTime")) {
                            continue;
                        }

                        if (!ObjectUtils.isEmpty(tableField) && !ObjectUtils.isEmpty(tableField.value())) {
                            tableFieldName = tableField.value();
                        }
                        int modifier = field.getModifiers();
                        if ((modifier | Modifier.STATIC) == modifier) {
                            continue;
                        }
                        field.setAccessible(true);
                        Object value = field.get(likeCondition);
                        if (!ObjectUtils.isEmpty(value) && !ObjectUtils.isEmpty(tableFieldName)) {
                            queryWrapper.like(tableFieldName, value);
                        }
                    }
                }
            }
        }

        if (!ObjectUtils.isEmpty(searchStartTime)) {
            queryWrapper
                    .ge("create_time", searchStartTime);
        }
        if (!ObjectUtils.isEmpty(searchEndTime)) {
            queryWrapper
                    .le("create_time", searchEndTime);
        }
        return queryWrapper;
    }


}
